<?php

class BlogModel extends Model {
	public function CheckInstallation() {
		try {
			$this->requireDB();
			$state = $this->sql->prepare("show tables like 'sparclemvc_blog'");
			$state->execute();
			return $state->fetch(PDO::FETCH_ASSOC);	
		} catch(Exception $e) {
			return false;
		}
	}
	
	public function Install() {
		$this->requireDB();
		try {
			$state = $this->sql->prepare("	CREATE TABLE `sparclemvc_blog` (
											`blog_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
											`blog_title` VARCHAR( 100 ) NOT NULL ,
											`blog_text` TEXT NOT NULL ,
											UNIQUE ( `blog_id` )) ENGINE = MYISAM ");
			$state->execute();
			$this->addEntry('First blog entrie','Welcome to this extremely simple blog example.');
			return true;
		} catch(Exception $e) {
			return false;
		}
	}
	
	public function Uninstall() {
		$this->requireDB();
		try {
			$state = $this->sql->prepare("	DROP TABLE `sparclemvc_blog`");
			$state->execute();
			return true;
		} catch(Exception $e) {
			return false;
		}
	}
	
	public function getBlogEntries() {
		$this->requireDB();
		$state = $this->sql->prepare("SELECT * FROM sparclemvc_blog ORDER BY blog_id DESC");
		$state->execute();
		return $state->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function addEntry($blog_title, $blog_text) {
		try {
			$state = $this->sql->prepare("INSERT INTO sparclemvc_blog (blog_title, blog_text) VALUES (:blog_title, :blog_text)");
			$state->bindParam(':blog_title', $blog_title, PDO::PARAM_STR);
			$state->bindParam(':blog_text', $blog_text, PDO::PARAM_STR);
			$state->execute();
			return true;
		} catch(Exception $e) {
			return false;
		}
		$this->requireDB();
	}
}
?>